@echo off
chcp 936 >nul
echo ========================================
echo    电商平台微服务启动脚本 - 批处理版
echo ========================================
echo.

:: 设置项目根目录
set PROJECT_ROOT=F:\shopping
echo 项目根目录: %PROJECT_ROOT%

:: 设置Nacos服务器地址
set NACOS_SERVER_ADDR=192.168.5.111:8848
set NACOS_GRPC_ADDR=192.168.5.111:9848
echo Nacos服务器地址: %NACOS_SERVER_ADDR%

:: 检查Java环境
echo 检查Java环境...
java -version
if %ERRORLEVEL% NEQ 0 (
    echo 错误: 未找到Java环境，请确保已安装Java并配置了JAVA_HOME环境变量
    pause
    exit /b 1
)
echo Java环境检查通过

:: 检查Maven环境
echo 检查Maven环境...
mvn -version
if %ERRORLEVEL% NEQ 0 (
    echo 错误: 未找到Maven环境，请确保已安装Maven并配置了MAVEN_HOME环境变量
    pause
    exit /b 1
)
echo Maven环境检查通过

echo.
echo 正在编译项目...
echo.

:: 编译common-module
echo [1/6] 编译common-module...
cd %PROJECT_ROOT%\common-module
call mvn clean install -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo 错误: common-module编译失败
    pause
    exit /b 1
)
echo common-module编译成功

:: 编译auth-service
echo [2/6] 编译auth-service...
cd %PROJECT_ROOT%\auth-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo 错误: auth-service编译失败
    pause
    exit /b 1
)
echo auth-service编译成功

:: 编译user-service
echo [3/6] 编译user-service...
cd %PROJECT_ROOT%\user-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo 错误: user-service编译失败
    pause
    exit /b 1
)
echo user-service编译成功

:: 编译product-service
echo [4/6] 编译product-service...
cd %PROJECT_ROOT%\product-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo 错误: product-service编译失败
    pause
    exit /b 1
)
echo product-service编译成功

:: 编译order-service
echo [5/6] 编译order-service...
cd %PROJECT_ROOT%\order-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo 错误: order-service编译失败
    pause
    exit /b 1
)
echo order-service编译成功

:: 编译gateway-service
echo [6/6] 编译gateway-service...
cd %PROJECT_ROOT%\gateway-service
call mvn clean package -DskipTests -q
if %ERRORLEVEL% NEQ 0 (
    echo 错误: gateway-service编译失败
    pause
    exit /b 1
)
echo gateway-service编译成功

echo.
echo 所有模块编译成功！
echo.
echo 正在启动服务...
echo.

:: 启动auth-service (端口8084)
echo 启动auth-service (端口: 8084)...
start "auth-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\auth-service && java -jar target\auth-service-1.0.0.jar"
timeout /t 8 /nobreak >nul

:: 启动user-service (端口8081)
echo 启动user-service (端口: 8081)...
start "user-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\user-service && java -jar target\user-service-1.0.0.jar"
timeout /t 8 /nobreak >nul

:: 启动product-service (端口8082)
echo 启动product-service (端口: 8082)...
start "product-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\product-service && java -jar target\product-service-1.0.0.jar"
timeout /t 8 /nobreak >nul

:: 启动order-service (端口8083)
echo 启动order-service (端口: 8083)...
start "order-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\order-service && java -jar target\order-service-1.0.0.jar"
timeout /t 8 /nobreak >nul

:: 启动gateway-service (端口8080)
echo 启动gateway-service (端口: 8080)...
start "gateway-service" cmd /k "chcp 65001 >nul && set NACOS_SERVER_ADDR=%NACOS_SERVER_ADDR% && set NACOS_GRPC_ADDR=%NACOS_GRPC_ADDR% && cd /d %PROJECT_ROOT%\gateway-service && java -jar target\gateway-service-1.0.0.jar"

echo.
echo ========================================
echo    所有服务启动完成！
echo ========================================
echo.
echo 服务状态：
echo - auth-service:    运行中 (端口: 8084)
echo - user-service:    运行中 (端口: 8081)
echo - product-service: 运行中 (端口: 8082)
echo - order-service:   运行中 (端口: 8083)
echo - gateway-service: 运行中 (端口: 8080)

echo.
echo 访问地址：
echo - 网关服务: http://localhost:8080
echo - Nacos控制台: http://%NACOS_SERVER_ADDR%/nacos
echo - 认证服务: http://localhost:8084
echo - 用户服务: http://localhost:8081
echo - 产品服务: http://localhost:8082
echo - 订单服务: http://localhost:8083

echo.
echo 管理命令：
echo - 查看服务日志: 在对应的命令窗口查看
echo - 停止服务: 关闭对应的命令窗口
echo - 重新启动: 再次运行此脚本

echo.
echo 按任意键退出此脚本（服务会继续运行）...
pause >nul